package com.bkck.sharkblockade.menu;

import com.bkck.sharkblockade.levels.LevelSelection;
import com.bkck.sharkblockade.levels.thread.MainThread;
import com.bkck.sharkblockade.menu.R;
import com.bkck.sharkblockade.highscores.Highscores;
import com.bkck.sharkblockade.highscores.ScoresDB;
import com.bkck.sharkblockade.instructions.Instructions;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.ScaleAnimation;
import android.widget.Button;

/**
 * The Menu page.
 * 
 * @author Team BKCK.
 * 
 */
public class Menu extends Activity {
  
  public static final String TAG = Menu.class.getSimpleName();
  
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // requesting to turn the title OFF
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    // making it full screen
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.menu);
    
    // map components to resources
    final Button play = (Button) findViewById(R.id.playButton);
    final Button instruction = (Button) findViewById(R.id.instructionsButton);
    final Button highscores = (Button) findViewById(R.id.highscoresButton);
    final Button exit = (Button) findViewById(R.id.exit_game);
    
    // animations
    final Animation fromLeft = AnimationUtils.makeInAnimation(Menu.this, true);
    final Animation fromRight = AnimationUtils.makeInAnimation(Menu.this, false);
    final Animation toLeft = AnimationUtils.makeOutAnimation(Menu.this, false);
    final Animation toRight = AnimationUtils.makeOutAnimation(Menu.this, true);
    final Animation scaleUp =
      new ScaleAnimation(1, 3, 1, 3, Animation.RELATIVE_TO_SELF, 0.5f,
          Animation.RELATIVE_TO_SELF, 0.5f);
    final Animation toTransparent = new AlphaAnimation(1, 0);
       
    // use animation set to to play multiple animations at once.
    final AnimationSet set = new AnimationSet(false);
    set.addAnimation(scaleUp);
    set.addAnimation(toTransparent);
    
    // durations for animations
    fromLeft.setDuration(1500);
    fromRight.setDuration(1500);
    toLeft.setDuration(200);
    toRight.setDuration(200);
    scaleUp.setDuration(300);
    toTransparent.setDuration(200);
    
    // entrance animation
    play.startAnimation(fromLeft);
    instruction.startAnimation(fromRight);
    highscores.startAnimation(fromLeft);
    exit.startAnimation(fromRight);

    // on click animations
    play.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
        //play.startAnimation(set);        
        play.startAnimation(toRight);       
        showLevelSelection(v);
      }
    });

    instruction.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {          
        instruction.startAnimation(toLeft);       
        showInstructions(v);
      }
    });

    highscores.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {          
        highscores.startAnimation(toRight);       
        showHighscores(v);
      }
    });
        
    exit.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {          
        exit.startAnimation(toLeft);       
        exitGame(v);
      }
    });
    
    // Creates instance of SQLite Database and saves it to the Main Thread
    // for future references.
    ScoresDB db = new ScoresDB(this.getApplicationContext());
    //ScoresDB.deleteAll();
    MainThread.setDB(db);
  }

  /**
   * Switch to the activity for level selection.
   * 
   * @param view The view.
   */
  public void showLevelSelection(View view) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setClassName(this, LevelSelection.class.getName());
    finish();
    startActivity(intent);
  }
  
  /**
   * Exit the game.
   * 
   * @param view The view.
   */
  public void exitGame(View view) {
    Log.d(TAG, "Exit button clicked");
    finish();
    super.onDestroy();    
  }

  /**
   * Switch to the activity for instructions
   * 
   * @param view The view.
   */
  public void showInstructions(View view) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setClassName(this, Instructions.class.getName());
    startActivity(intent);
  }

  /**
   * Switch to the activity for highscores.
   * 
   * @param view The view.
   */
  public void showHighscores(View view) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setClassName(this, Highscores.class.getName());
    finish();
    startActivity(intent);
  }

  /**
   * Destorys this activity when onStop is called.
   */
  @Override
  protected void onStop() {
    finish();
    super.onDestroy();   
  }

  /**
   * Ignore screen orientation change.
   */
  @Override
  public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
  }
}